package com.infore.exchange.main.dao;

import com.infore.common.model.dto.exchange.TransformInfoDto;
import com.infore.common.model.entity.exchange.TTransformInfo;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.infore.common.request.query.exchange.TTransformInfoRequest;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数据交换 Mapper 接口
 * </p>
 *
 * @author lijh
 * @since 2023-02-10
 */
@Mapper
@Component
public interface TTransformInfoMapper extends BaseMapper<TTransformInfo> {


    @Select("select source_id, database_id, data_table, data_fields, where_condition, increment_column, increment_start_value, increment_end_value, url " +
            "from t_ex_source_data_info where source_id=#{id}")
    Map<String,Object> querySourceDataInfoById(@Param("id")  String id);


    @Select("select target_id, database_id, data_table, data_fields " +
            "from t_ex_source_data_info where target_id=#{id}")
    Map<String,Object> queryTargetDataInfoById(@Param("id")  String id);


    @Select("select * from (select count(1) as jobNum from t_ex_job_info ) A\n" +
            "left join (select count(1) as taskNum from t_ex_task_info) B ON 1=1\n" +
            "left join (select count(distinct schedul_id) as totalSchedulTime from t_ex_transform_log) C ON 1=1\n" +
            "left join (select count(1) as totalTaskTime from t_ex_transform_log) D ON 1=1")
    Map<String,Object> queryJobAndTaskStatistical();

    @Select("<script>" +
            "select ti.transform_id as transformId,ti.transform_name as transformName,ti.transform_type as transformType,\n" +
            "       ti.state ,ti.description,ti.create_time as createTime,\n" +
            "       sdi.source_id as sourceId,sdi.database_id as sourceDatabaseId,sdi.data_table as sourceTable,sdi.data_fields as sourceFields,sdi.where_condition as sourceWhere,\n" +
            "       sdi.increment_column as sourceIncrementCol,sdi.increment_type as sourceIncrementType, sdi.increment_start_value as sourceIncrementStartValue," +
            "       sdi.increment_before_minutes as sourceIncrementBeforeMinutes, \n" +
            "       sdi.increment_end_value as sourceIncrementEndValue,sdi.url as sourceUrl,url_method as sourceUrlMethod, \n" +
            "       tdi.target_id as targetId,tdi.database_id as targetDatabaseId,tdi.data_table as targetTable,tdi.data_fields as targetFields,\n" +
            "       tdi.increment_column as targetIncrementCol, \n"+
            "       dis.database_name as sourceDatabaseName,dit.database_name as targetDatabaseName\n" +
            "from t_ex_transform_info ti\n" +
            "left join t_ex_source_data_info sdi on ti.source_id = sdi.source_id\n" +
            "left join t_ex_target_data_info tdi on ti.target_id = tdi.target_id\n" +
            "left join t_ex_database_info dis on sdi.database_id = dis.database_id\n" +
            "left join t_ex_database_info dit on tdi.database_id = dit.database_id\n" +
            "where ti.transform_id=#{id} " +
            "</script>")
    TransformInfoDto queryTransformInfoById(@Param("id")  String id);

    @Select("<script>" +
            "select ti.transform_id as transformId,ti.transform_name as transformName,ti.transform_type as transformType,\n" +
            "       ti.state ,ti.description,ti.create_time as createTime,\n" +
            "       sdi.source_id as sourceId,sdi.database_id as sourceDatabaseId,sdi.data_table as sourceTable,sdi.data_table_cn as sourceTableCn, sdi.data_fields as sourceFields,sdi.where_condition as sourceWhere,\n" +
            "       sdi.increment_column as sourceIncrementCol,sdi.increment_type as sourceIncrementType, sdi.increment_start_value as sourceIncrementStartValue," +
            "       sdi.increment_end_value as sourceIncrementEndValue,sdi.url as sourceUrl,\n" +
            "       tdi.target_id as targetId,tdi.database_id as targetDatabaseId,tdi.data_table as targetTable,tdi.data_table_cn as targetTableCn,tdi.data_fields as targetFields,\n" +
            "       tdi.increment_column as targetIncrementCol, \n"+
            "       dis.database_name as sourceDatabaseName,dit.database_name as targetDatabaseName\n" +
            "from t_ex_transform_info ti\n" +
            "left join t_ex_source_data_info sdi on ti.source_id = sdi.source_id\n" +
            "left join t_ex_target_data_info tdi on ti.target_id = tdi.target_id\n" +
            "left join t_ex_database_info dis on sdi.database_id = dis.database_id\n" +
            "left join t_ex_database_info dit on tdi.database_id = dit.database_id\n" +
            "where 1=1 " +
            " <if test='transformId != null and transformId != \"\"'> "+
            "  and ti.transform_id=#{transformId} " +
            " </if> " +
            " <if test='transformName != null and transformName != \"\"'> "+
            "  and ti.transform_name like '%${transformName}%'  " +
            " </if> " +
            " <if test='transformType != null and transformType != \"\"'> "+
            "  and ti.transform_type=#{transformType} " +
            " </if> " +
            " <if test='sourceDatabase != null and sourceDatabase != \"\"'> "+
            "  and dis.database_name=#{sourceDatabase} " +
            " </if> " +
            " <if test='targetDatabase != null and targetDatabase != \"\"'> "+
            "  and dit.database_name=#{targetDatabase} " +
            " </if> " +
            " <if test='sourceTable != null and sourceTable != \"\"'> "+
            "  and sdi.data_table=#{sourceTable} " +
            " </if> " +
            " <if test='targetTable != null and targetTable != \"\"'> "+
            "  and tdi.data_table=#{targetTable} " +
            " </if> " +
            "</script>")
    List<TransformInfoDto> queryTransformInfo(TTransformInfoRequest  request);

}
